//////////////////////////////////////
// Fast Container Library           //
// MIT License - Copyright (c) 2012 //
//////////////////////////////////////

#pragma once

#include "allocator.h"

FC_NAMESPACE_BEGIN


// static_allocator
//
// An allocator that simply holds a static buffer of stack-based memory,
// essentially transforming any object or container using this allocator
// as a static_container of that type. This has a few downsides to
// inherent static or fixed-sized containers and fixed allocators, and
// care must be taken that memory requests never exceed the capicity.
//
// static_allocator will also never return any pointer other
// than to the beginning of its internal buffer.

template <size_t N, size_t Alignment = FC_DEFAULT_ALIGN>
class static_allocator
{
public:
	enum
	{
		buffer_size_n_bytes = N,
		buffer_alignment = Alignment
	};

	typedef aligned_buffer<N, Alignment> buffer_type;

	void* allocate( size_t n, size_t /*alignment*/ = 0 )
	{
		FC_ASSERT(n <= N);
		return (void*)m_buffer.data;
	}

	void deallocate( void* ptr, size_t /*size*/ = 0 )
	{
		FC_ASSERT(ptr == (void*)m_buffer.data);
	}

	static_allocator& operator =( const static_allocator& ) { return *this; }

	bool operator ==( const allocator& ) { return false; }
	bool operator !=( const allocator& ) { return true; }

	bool operator ==( const static_allocator& ) { return false; }
	bool operator !=( const static_allocator& ) { return true; }

protected:
	buffer_type m_buffer;

};



FC_NAMESPACE_END


